var ispass = new Array();
//初始化表单判断数组
for(var i=0;i<10;i++){
	ispass[i] = true;
}
window.onload=function(){
	if(typeof(document.getElementsByClassName('mycenter_icon')[0])!="undefined"){
		document.getElementsByClassName('mycenter_icon')[0].onmousemove = function(){
			document.getElementsByClassName('mycenter_list')[0].style.display = "block";
		}
		document.getElementsByClassName('mycenter_icon')[0].onmouseout = function(){
			document.getElementsByClassName('mycenter_list')[0].style.display = "none";
		}
		document.getElementsByClassName('mycenter_list')[0].onmousemove = function(){
			document.getElementsByClassName('mycenter_list')[0].style.display = "block";
		}
		document.getElementsByClassName('mycenter_list')[0].onmouseout = function(){
			document.getElementsByClassName('mycenter_list')[0].style.display = "none";
		}
	}
	if(typeof(pagekind)!="undefined"){
		if(pagekind=="viewport"){

		}
		else if(pagekind=="mycenter"){
			document.getElementById('submit').onclick = function(){
				event.preventDefault();
				//点击提交按钮自动完成验证
				if(document.getElementById('oldpassword').value==''){
					document.getElementById('warning2').innerHTML = "填写原密码才能修改信息哦";
					document.getElementById('warning2').style.display = 'block';
					document.getElementById('oldpassword').style.border = "1px solid red"
					return;
				}
				var pass = true;
				for(var i=0;i<ispass.length;i++){
					if(ispass[i]==false){
						pass = false;
						break;
					}
				}
				if(pass=true){
					updateUserData();
				}
			}
			//上传头像
			document.getElementById('imginput').onchange = function(){
				var xmlHttp = createXmlHttp();
				var url = './trans.php?action=subhead';
			 	var postData = "";
			  	var form = new FormData();
			  	form.append("file",document.getElementById("imginput").files[0]);
			  	xmlHttp.open("POST", url, true);
			  	xmlHttp.onreadystatechange = function() {
				if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {	
				  if(xmlHttp.responseText!='') {
				  		document.getElementById('headimg').src="source/head/"+xmlHttp.responseText;
				  }
				}
			  }
				xmlHttp.send(form);						
			}

			var node = document.getElementById('oldpassword');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(2,this.value)==false){
						document.getElementById('warning2').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[0] = false
						document.getElementById('warning2').innerHTML = "请输入6-16位字母、数字或特殊符号";
					}else{
						document.getElementById('warning2').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						ispass[0] = true
					}
				}else{
					ispass[0] = true;
				}
			}

			node = document.getElementById('password');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(2,this.value)==false){
						document.getElementById('warning3').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[1] = false
					}else{
						document.getElementById('warning3').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						document.getElementById('passwordagin').onblur()
						ispass[1] = true
					}						
				}
			}			

			node = document.getElementById('passwordagin');
			node.onblur = function(){
				var str = document.getElementById('newpassword').value;
				if(str!=this.value){
					document.getElementById('warning4').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[2] = false
				}else{
					document.getElementById('warning4').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"
					ispass[2] = true		
				}
			}

			node = document.getElementById('qq');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(3,this.value)==false){
						document.getElementById('warning6').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[3] = false
					}else{
						document.getElementById('warning6').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						ispass[3] = true
					}						
				}
			}			

			node = document.getElementById('mail');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(4,this.value)==false){
						document.getElementById('warning5').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[4] = false
					}else{
						document.getElementById('warning5').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						ispass[4] = true
					}						
				}
			}

			node = document.getElementById('website');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(5,this.value)==false){
						document.getElementById('warning7').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[5] = false
					}else{
						document.getElementById('warning7').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						ispass[5] = true
					}						
				}
			}
		}else if(pagekind=="register"){
			document.getElementById('submit').onclick = function(){
				event.preventDefault();
				var pass = true;
				for(var i=0;i<ispass.length;i++){
					if(ispass[i]==false){
						pass = false;
						break;
					}
				}
				if(pass=true){
					subUserData();
				}
			}

			var node = document.getElementById('username');
			node.onblur = function(){
				if(checkinput(1,this.value)==false){
					document.getElementById('warning1').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[0] = false
				}else{
					document.getElementById('warning1').innerHTML = "请输入3-16位汉字、字母或数字";
					document.getElementById('warning1').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"	
					ispass[0] = true
				}
				if(this.value!=''){
					checkname(this.value,1)
				}
			}
			node = document.getElementById('password');
			node.onblur = function(){
				if(checkinput(2,this.value)==false){
					document.getElementById('warning2').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[1] = false
				}else{
					document.getElementById('warning2').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"	
					document.getElementById('passwordagine').onblur()
					ispass[1] = true
				}		
			}
			node = document.getElementById('passwordagine');
			node.onblur = function(){
				var str = document.getElementById('password').value;
				if(str!=this.value){
					document.getElementById('warning3').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[2] = false
				}else{
					document.getElementById('warning3').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"
					ispass[2] = true		
				}
			}
			node = document.getElementById('qq');
			node.onblur = function(){
				if(this.value!=''){
					if(checkinput(3,this.value)==false){
						document.getElementById('warning4').style.display = 'block';
						this.style.border = "1px solid red"
						ispass[3] = false
					}else{
						document.getElementById('warning4').style.display = 'none';
						this.style.border = "1px solid #e2e2e2"	
						ispass[3] = true
					}		
				}
			}
		}else if(pagekind=="login"){
			var node = document.getElementById('username');
			node.onblur = function(){
				if(checkinput(1,this.value)==false){
					document.getElementById('warning1').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[0] = false
				}else{
					document.getElementById('warning1').innerHTML = "请输入3-16位汉字、字母或数字";
					document.getElementById('warning1').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"	
					ispass[0] = true
				}
				if(this.value!=''){
					checkname(this.value,2)
				}
			}
			node = document.getElementById('password');
			node.onblur = function(){
				if(checkinput(2,this.value)==false){
					document.getElementById('warning2').style.display = 'block';
					this.style.border = "1px solid red"
					ispass[1] = false
				}else{
					document.getElementById('warning2').style.display = 'none';
					this.style.border = "1px solid #e2e2e2"	
					document.getElementById('passwordagine').onblur()
					ispass[1] = true
				}		
			}
		}
	}
}

function checkinput(kind,str){
	var match
	if(kind==1){//用户名
		match = /^[\w\u4e00-\u9fa5]{2,16}$/;
	}else if(kind==2){//密码
		match = /^[\S]{6,16}$/;
	}else if(kind==3){//QQ
		match = /^[\d]{5,12}$/;
	}else if(kind==4){//邮箱
		match = /^[\w]{1,20}@[\w]{1,20}\.[A-Za-z]{1,10}$/
	}else if(kind==5){//网址
		match = /^(http|https):\/\/[\w]{0,20}\.?[\w]{1,20}\.[\w]{1,10}\/$/
	}
	return match.test(str)
}
function createXmlHttp() {
  var xmlHttp = null;
  try {
	xmlHttp = new XMLHttpRequest();
  } catch (e) {
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
  return xmlHttp;
}
function checkname(username,kind){
	var xmlHttp = createXmlHttp();
	var url = './trans.php?action=checkname';
	var form = new FormData();
	form.append('username',username);
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = function() {
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	  if(kind==1&&xmlHttp.responseText!=1){
	  		document.getElementById('warning1').style.display = 'block';
			document.getElementById('warning1').innerHTML = '该用户名已注册';
			document.getElementById('username').style.border = "1px solid red"
			ispass = false
	  }else if(kind==2&&xmlHttp.responseText==1){
	  		document.getElementById('warning1').style.display = 'block';
			document.getElementById('warning1').innerHTML = '该用户名不存在';
			document.getElementById('username').style.border = "1px solid red"
			ispass = false	  	
	  }
	}
  }
	xmlHttp.send(form);										
}
function subUserData(){
	var xmlHttp = createXmlHttp();
	var url = './trans.php?action=register';
	var form = new FormData(document.getElementById('form'));
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = function() {
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	  if(xmlHttp.responseText==1){
	  	 location.replace('index.php') 
	  }
	}
  }
	xmlHttp.send(form);			
}
function updateUserData(){
	var xmlHttp = createXmlHttp();
	var url = './trans.php?action=updatedata';
	var form = new FormData(document.getElementById('form'));
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = function() {
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	  if(xmlHttp.responseText==1){
	  	 location.replace('mycenter.php')
	  }else if(xmlHttp.responseText==7){
	  	document.getElementById('warning2').innerHTML = "原密码错误";
		document.getElementById('warning2').style.display = 'block';
		document.getElementById('oldpassword').style.border = "1px solid red"
	  }else{
	  	alert(xmlHttp.responseText)
	  	alert("修改失败，未知错误")
	  }
	}
  }
	xmlHttp.send(form);			
}
//上传评论
function submitForm(articleid,username){
	  var xmlHttp = createXmlHttp();
	  if(!xmlHttp){
		return 0;
	  }
	  var url = './trans.php?action=upremark';
	  var postData = "";
	  postData += "artid="+articleid;
	  if(username==''){
		  postData += "&name="+document.getElementById("name").value;;
		  postData += "&email=" + document.getElementById("email").value;
		  postData += "&url=" + document.getElementById("url").value;		  	
	  }
	  postData += "&content=" + document.getElementById("content").value;
	  xmlHttp.open("POST", url, true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		  if(xmlHttp.responseText == '1') {
			//alert('post successed');
			location.reload(true)
		  }
		  
		}
	  }
	  xmlHttp.send(postData);
}
//删除评论
function deleteRemark(id,articleid){
	  var xmlHttp = createXmlHttp();
	  if(!xmlHttp) {
		return 0;
	  }
	  var url = './admin/trans.php?action=deleteremark';	
	  var postData = "";
	  postData += "id="+id;
	  postData += "&artid="+articleid;
	  xmlHttp.open("POST", url, true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		  if(xmlHttp.responseText == '1') {
			location.reload(true)
		  }
		  
		}
	  }
	  xmlHttp.send(postData);
}
//获得评论
function getRemark(artid,page){
	var xmlHttp = createXmlHttp();
	var url
	if(!xmlHttp) {
		return 0;
	}
	if(typeof(page)!="undefined"){
		url = 'trans.php?action=getremarkpage&artid='+artid+'&page='+page;
	}else{
		url = 'trans.php?action=getremarkpage&artid='+artid
	}
	  xmlHttp.open("POST", url, true);
	  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	  xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		  if(xmlHttp.responseText) {
			var data = eval(xmlHttp.responseText);
			setRemark(data,artid,page);
		  }
		}
	  }
	  xmlHttp.send('');	
}
function setRemark(data,artid,page){
	var node = document.getElementsByClassName('page')[0];
	node.innerHTML = ''
	for(var i=0;i<data[0].length;i++){
		var newNode = document.createElement("a");
		newNode.setAttribute('class','pageNum');
		newNode.setAttribute('onclick','getRemark('+artid+','+(data[0][i])+')');
		newNode.innerHTML = data[0][i];
		node.appendChild(newNode)
	}
	node = document.getElementsByClassName('remarkContent')[0];
	node.innerHTML = ''
	for(var i=0;i<data[2].length;i++){
		//主框架
		var newNode = document.createElement("div");
		newNode.setAttribute('class','remarkOne');

		//信息框架
		var tempNode0 = document.createElement("div");
		tempNode0.setAttribute('class','remarkInfo');

		//头像
		var tempNode1 = document.createElement("img");
		tempNode1.src = "source/head/";
		if(typeof(data[2][i]['headpic']) != "undefined" && data[2][i]['headpic']!=null){
			tempNode1.src = tempNode1.src+data[2][i]['headpic'];
		}else{
			tempNode1.src = tempNode1.src+"none.png";
		}
		tempNode0.appendChild(tempNode1);
		//名字
		tempNode1 = document.createElement("a");
		tempNode1.setAttribute('class','name');
		tempNode1.innerHTML = data[2][i]['name']
		tempNode0.appendChild(tempNode1);
		//发布时间
		tempNode1 = document.createElement("div");
		tempNode1.setAttribute('class','time');
		tempNode1.innerHTML = "发表于 "+data[2][i]['pubtime']
		tempNode0.appendChild(tempNode1);


		newNode.appendChild(tempNode0);

		tempNode0 = document.createElement("div");
		tempNode0.setAttribute('class','remarkText');
		tempNode0.innerHTML = data[2][i]['content']
		newNode.appendChild(tempNode0);


		node.appendChild(newNode)
	}


	
}